// ------
// @Nirvana Toolkit
// description: 可挂载多个SkillAction的Action，多个Action按照顺序同帧执行
// ------

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEditor;
using Newtonsoft.Json;

namespace Nirvana.SkillEditor
{
    [Serializable]
    public class MultiSkillActionInspector
    {
        public List<SkillActionBase> actions = new List<SkillActionBase>();
        
        public void AddSkillAction(Type type)
        {
            var newSkillAction = (SkillActionBase) Activator.CreateInstance(type);
            actions.Add(newSkillAction);
        }

        public void RemoveSkillAction(SkillActionBase action)
        {
            if (actions.Contains(action))
            {
                actions.Remove(action);
            }
        }
        
        public void PrepareFrame(Playable playable, FrameData info)
        {
            foreach (var action in actions)
            {
                action.PrepareFrame(playable, info);
            }
        }

        public void ProcessFrame(Playable playable, FrameData info, object playerData)
        {
            foreach (var action in actions)
            {
                action.ProcessFrame(playable, info, playerData);
            }
        }

        public void OnGUI()
        {
            if (GUILayout.Button("Add New Action"))
            {
                EditorUtils.ShowChildTypeGenericMenu("Skill Actions", typeof(SkillActionBase), AddSkillAction);
            }

            if (actions.Count > 1)
            {
                EditorGUILayout.HelpBox("多个Action会按照id顺序同帧执行", MessageType.Warning);
            }

            for (int i = 0; i < actions.Count; i++)
            {
                var action = actions[i];
                action.foldout = EditorUtils.Foldout(action.foldout, $"ID:{i}\t{action.title}");
                var rect = GUILayoutUtility.GetLastRect();
                rect.x = rect.x + rect.width - 52;
                rect.width = 50;
                rect.y += 2;
                rect.height -= 4;
                if (GUI.Button(rect, "Delete"))
                {
                    RemoveSkillAction(action);
                }

                rect.x -= 23;
                rect.width = 21;
                if (GUI.Button(rect, "▼"))
                {
                    if (i < actions.Count - 1)
                    {
                        var t = actions[i];
                        actions[i] = actions[i + 1];
                        actions[i + 1] = t;
                    }
                }

                rect.x -= 23;
                if (GUI.Button(rect, "▲"))
                {
                    if (i > 0)
                    {
                        var t = actions[i];
                        actions[i] = actions[i - 1];
                        actions[i - 1] = t;
                    }
                }

                if (action.foldout)
                {
                    action.OnGUI();
                }
            }
        }

        public void SetDelayAndDuration(double delay, double duration)
        {
            foreach (var action in actions)
            {
                action.SetDelayAndDuration(delay, duration);
            }
        }
    }
}